<template>
  <div>
    <el-form ref="configform" :model="configData" :rules="configRules" label-width="120px">
      <el-form-item label="优惠比列" prop="ratio">
        <el-input v-model="configData.discount_ratio" placeholder="请输入优惠比例" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="赠送优惠卷数量" prop="count"> 
        <el-input v-model="configData.voucher_count" placeholder="请输入赠送数量" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="代金券类型">
        <el-select v-model="configData.voucher_type_id" placeholder="请选择有效时间" size="small" style="max-width:70">
          <el-option :label="item.name" :value="item.id" v-for="item in shopVouList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="获取积分倍数" prop="score">
        <el-input v-model="configData.score_times" placeholder="请输入积分倍数" size="small" style="max-width:70%"></el-input>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { getVipConfig, getShopVouType, putVipConfig, postVipConfig } from '@/api/vip'

export default {
  name: 'vipConfig',
  data() {
    return {
      configData: {
        key: this.$store.state.app.activeApp.saa_key,
        discount_ratio: '',
        voucher_count: '',
        score_times: '',
        voucher_type_id: '',
        status: 1
      },
      shopVouList: [],

      isAdd: true,

      configRules: {
        ratio: [
          { required: false, message: '请输入优惠比例', trigger: 'blur' },
        ],
        count: [
          { required: false, message: '请输入代金券数量', trigger: 'blur' },
        ],
        score: [
          { required: false, message: '请输入会员积分倍数', trigger: 'blur' },
        ]
      }
    }
  },
  watch: {
    shopVouList(newData) {
      if(newData.length>0){
        this.configData.voucher_type_id = newData[0].id;
      }
    }
  },
  mounted() {
    this.getVipConfig();
    this.getShopVouType();
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.configform.validate((valid) => {
        if(valid) {
          if(!this.isAdd){
            const params = {
              key: this.$store.state.app.activeApp.saa_key,
              id: this.configData.id,
              discount_ratio: this.configData.discount_ratio,
              voucher_count: this.configData.voucher_count,
              voucher_type_id: this.configData.voucher_type_id,
              score_times: this.configData.score_times
            };
            putVipConfig(params).then(res => {
              if(res.status === 200){
                this.$emit('configSuccess');
                this.$message.success('修改成功！')
              }else{
                this.$message.error(res.message);
              }
            })
          }else{
            const params = {
              key: this.$store.state.app.activeApp.saa_key,
              discount_ratio: this.configData.discount_ratio,
              voucher_count: this.configData.voucher_count,
              voucher_type_id: this.configData.voucher_type_id,
              score_times: this.configData.score_times
            };
            postVipConfig(params).then(res => {
              if(res.status === 200){
                this.$emit('configSuccess');
                this.$message.success('添加成功！')
              }else{
                this.$message.error(res.message);
              }
            })
          }
        }else{
          console.log(this.configData)
        }
      })
    },
    /**
     * 获取会员卡配置
     */
    getVipConfig() {
      getVipConfig({ key: this.$store.state.app.activeApp.saa_key }).then(response => {
        if(response.status === 200){
          this.configData = response.data;
          this.isAdd = false;
        }else if(response.status === 204){
          this.configData = {
            key: this.$store.state.app.activeApp.saa_key,
            discount_ratio: '',
            voucher_count: '',
            score_times: '',
            voucher_type_id: '',
            status: true
          };
          this.isAdd = true;
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 获取代金券类型
     */
    getShopVouType() {
      getShopVouType({ key: this.$store.state.app.activeApp.saa_key, type: '1' }).then(response => {
        if(response.status === 200){
          this.shopVouList = response.data;
        }else{
          this.$message.error(response.message);
        }
      })
    }
  },
}
</script>